<template>
  <el-container class="home-container">
    <!-- 顶栏区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体部分区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff" unique-opened active-text-color="#8e6fd6" :collapse='isCollapse' :collapse-transition='false' :router="true" :default-active='activePath'>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key='item.id' >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key='subItem.id' @click="saveNavState(subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data: function () {
    return {
      // 左侧菜单列表
      menuList: [],
      iconObj: {
        125: 'iconfont icon-guanliyuan',
        103: 'iconfont icon-BBDhezi',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-icon',
        145: 'iconfont icon-tongjitubiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created: function () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 点击退出，回到登录界面
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 请求左侧菜单列表
    getMenuList: async function () {
      const res = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.warning('左侧菜单列表获取失败')
      }
      this.menuList = res.data
    },
    // 点击按钮，折叠左侧菜单
    toggleCollapse: function () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState: function (info) {
      window.sessionStorage.setItem('activePath', info)
      this.activePath = info
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    > div {
      display: flex;
      align-items: center;
      span {
        color: #fff;
        font-size: 20px;
        // line-height: 60px;
        margin-left: 20px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
  .el-body {
    background-color: #eaedf1;
  }
  .el-main {
    background-color: #EAEDF1;
  }
  .iconfont {
    font-size: 16px;
    margin-right: 14px;
  }
  .icon-guanliyuan {
    color: lightskyblue;
  }
  .icon-BBDhezi {
    color: #76c176;
  }
  .icon-shangpinguanli {
    color: #de8a69;
  }
  .icon-icon {
    color: #f7f5f5;
  }
  .icon-tongjitubiao {
    color: #4499f3;
  }
  .el-menu {
    border-right: 0px;
  }
}
.toggle-button {
  text-align: center;
  background-color: #4A5064;
  color: #fff;
  line-height: 24px;
  font-size: 10px;
  letter-spacing: 4px;
  cursor: pointer;
}
</style>
